<?php
/**
 * @author spenceryg
 * @date 2022/11/29
 * @time 9:27 下午
 * @focus love & life
 */


namespace app\auto\controller;


use app\auto\business\RelationBusiness;
use app\auto\lib\ModelGenerator;
use think\facade\View;
use think\response\Json;

class Relation extends \app\BaseController
{
    public function index()
    {
        $data = \app\auto\model\Relation::select()->append(['masterName', 'sliverName', 'middleName', 'typeName']);
        View::assign('data', $data);
        return View::fetch();
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            RelationBusiness::add($data);
            return success();
        } else {
            View::assign('types', RelationBusiness::$types);
            View::assign('tables', \app\auto\model\Table::select());
            return View::fetch();
        }
    }


    public function delete()
    {
        $id = input('id');
        RelationBusiness::delete($id);
        return success();
    }

    public function generate()
    {
        ModelGenerator::generate();
        return success();
    }


    public function code()
    {
        $tables = \app\auto\model\Table::select();
        View::assign('tables', $tables);
        return View::fetch();
    }


    /**
     * 获取最大关联
     * getFullLinks
     * @return mixed|Json
     */
    public function getFullLinks()
    {
        $table_name = input('table_name');
        if (!$table_name) {
            return error('主表必须');
        }
        $full = RelationBusiness::getModelFullRelation($table_name);
        return $full['full_str'];
    }


    /**
     *  获取最小join_in路径
     * getDbMini
     * @return string|Json
     */
    public function getDbMini()
    {
        $table_name = input('table_name');
        if (!$table_name) {
            return error('主表必须');
        }
        $ids = input('ids');
        return RelationBusiness::getDbMini($table_name, $ids);
    }


    /**
     * 根据表名获取字段options
     * getFieldOptionsByTableName
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFieldOptionsByTableName(): string
    {
        $table_name = input('table_name');
        $table_name_no_prefix = str_replace(env('DATABASE.PREFIX'), '', $table_name);
        $table = \app\auto\model\Table::getByName($table_name);
        $table_id = $table['id'];
        $fields = \app\auto\model\Field::where('table_id', $table_id)->select();
        $html = '<option value="">请选择</option>';
        foreach ($fields as $field) {
            $html .= <<<EOF
<option value="{$field['name']}" data-alias="{$table_name_no_prefix}__{$field['name']}">{$field['comment']}</option>
EOF;
        }
        return $html;
    }


    /**
     * 根据条件获取代码
     * getCode
     * @return Json
     */
    public function getCode()
    {
        $data = input();
        $html = RelationBusiness::getCode($data);
        return success('', $html);
    }






}
